<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/12
 * Time: 16:51
 */

namespace app\admin\model;


use think\Model;

class Gift extends Model
{
    public function getList($keyword,$pageNum, $num_per_page)
    {
        $list = $this->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->where('gift_name', 'like', "%{$keyword}%");
                }
            })
            ->where('is_delete',0)
            ->page($pageNum, $num_per_page)
            ->order('update_time desc')
            ->select();
        return $list;
    }

    public function getCount($keyword)
    {
        $count = db('gift')
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->where('gift_name', 'like', "%{$keyword}%");
                }
            })
            ->where("is_delete",0)
            ->count();
        return $count;
    }

    public function doAdd($info)
    {
        $info['create_time'] = $info['update_time'] = time();
        $result = db('gift')
            ->insert($info);
        if (!$result) {
            exception('添加失败');
        }
    }

    public function modify($id)
    {
        $info = db('gift')
            ->where('id', $id)
            ->find();
        return $info;
    }

    public function doEdit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        $info['update_time'] = time();
        $result = db('gift')
            ->where('id',$id)
            ->update($info);
        if(!$result){
            exception('修改失败');
        }
    }
    /**
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function doDelete($id){
        $result = db('gift')
            ->where('id',$id)
            ->delete();
        if(!$result){
            exception("删除失败");
        }
    }
}